/*************************************************************
 * File: Menu.java
 * Copyright (c) 2012
 * Author: Huang Shaobin
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com 
 * Date: Feb 20, 2013
 * Description: 
 ************************************************************/

package snow.biz.system.model;

import org.apache.commons.lang.StringUtils;

import snow.biz.basic.model.Persistence;
import snow.biz.security.model.Privilege;
import snow.core.enums.Status;
import snow.core.exceptions.BusinessException;
import snow.core.toolkit.Validator;

/**
 * 系统菜单
 * 
 * @author Shaobin.Software@gmail.com Shaobin_Software@163.com
 * @version 1.0
 */
@SuppressWarnings("serial")
public class Menu extends Persistence<Integer> {

	private String text; // 菜单名称
	private Boolean leaf; // 是否为叶子节点
	private String treeRelation; // 树形关系
	private Menu superior; // 上级节点
	private String component; // 组件信息
	private Integer sort; // 排序字段
	private Privilege privilege; // 对应权限

	protected Menu() {
	}

	public Menu(User createUser, Status status, String text, Boolean leaf, Menu superior, String component, Integer sort, Privilege privilege)
			throws BusinessException {
		super(createUser, status);
		setText(text);
		setComponent(component);
		setLeaf(leaf);
		setPrivilege(privilege);
		setSort(sort);
		setSuperior(superior);
	}

	public String getComponent() {
		return this.component;
	}

	public Boolean getLeaf() {
		return this.leaf;
	}

	public Privilege getPrivilege() {
		return this.privilege;
	}

	public Integer getSort() {
		return this.sort;
	}

	public Menu getSuperior() {
		return this.superior;
	}

	public String getText() {
		return this.text;
	}

	public String getTreeRelation() {
		if (StringUtils.isBlank(this.treeRelation)) {
			resetTreeRelation();
		}
		return this.treeRelation;
	}

	public void resetTreeRelation() {
		if (getId() != null) {
			final StringBuilder treeRelation = new StringBuilder();
			final Menu superior = getSuperior();
			if (superior != null) {
				treeRelation.append(superior.getTreeRelation());
			} else {
				treeRelation.append(',');
			}
			treeRelation.append(getId()).append(',');
			this.treeRelation = treeRelation.toString();
		}
	}

	public void setComponent(String component) {
		this.component = component;
	}

	public void setLeaf(Boolean leaf) {
		this.leaf = leaf;
	}

	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public void setSuperior(Menu superior) {
		this.superior = superior;
	}

	public void setText(String text) throws BusinessException {
		Validator.notBlank(text, "", "");
		this.text = text;
	}

}
